package com.module.TraceSource.manager;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.TraceSource.dao.TsCodeForRecordDao;
import com.module.TraceSource.dao.TsKindDao;
import com.module.TraceSource.model.TraceSourceBeanUtil;
import com.module.TraceSource.util.ReturnUtil;
import com.module.TraceSource.vo.TsCodeForRecord;
import com.module.TraceSource.vo.TsKind;

public class TsCodeForRecordManager extends GenericHibernateManager<TsCodeForRecord,Long>{

	@Override
	public GenericHibernateDao<TsCodeForRecord, Long> getDao() {
		// TODO Auto-generated method stub
		return null;
	}
	private static TsCodeForRecordManager service;
	private TsCodeForRecordDao dao = TsCodeForRecordDao.getInstance();
	private TsKindDao kindDao = TsKindDao.getInstance();
	public synchronized static TsCodeForRecordManager getInstance(){
		if(service == null){
			service = new TsCodeForRecordManager();
		}
		return service;
	}
	
	/**
	 * 溯源信息管理列表查询
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public Page listTsCodeForRecordPage(TsCodeForRecord bean,Page page)throws Exception{
		Session session=getSession();
		try {
			page = dao.getList(session, bean, page.getStart(), page.getLimit());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.closeSession();
		}
		return page;
	}
	/**
	 * 新增 溯源信息管理
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> addTsCodeForRecord(TsCodeForRecord bean)throws Exception{
		Session session = this.getSession();
 		Transaction tx = session.beginTransaction();
 		boolean flag = false;
 		Map<String,Object> map = new HashMap<String,Object>();
 		try {
 			bean.setState("000001");
 			String kindName=bean.getKindName();
 			boolean isExist=dao.isExistRecord(session, bean.getName(),bean.getOrgId());
 			if(isExist==true){
 				map=ReturnUtil.optCodeErrorHandle(bean.getName()+"已经存在!");
 			}else{
 				//根据名称获得id
 	 			Long kindId=kindDao.findIdByName(session, kindName);
 	 			if(kindId!=null){
 	 				bean.setKindId(kindId);
 	 			}
 				flag = dao.save(session, bean);
 				tx.commit();
 				map = ReturnUtil.optHandle(flag);
 			}
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally {
			this.closeSession();
		}
		return map;
	}
	/**
	 * 编辑 溯源信息管理
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> updateTsCodeForRecord(TsCodeForRecord bean)throws Exception{
		Session session = this.getSession();
 		Transaction tx = session.beginTransaction();
 		boolean flag = false;
 		Map<String,Object> map = new HashMap<String,Object>();
 		try {
			TsCodeForRecord vo=dao.get(session, bean.getId());
			String kindName=bean.getKindName();
			Long kindId=kindDao.findIdByName(session, kindName);
			if(kindId!=null){
 				vo.setKindId(kindId);
 			}
			vo.setName(bean.getName());
			vo.setDesc(bean.getDesc());
			flag = dao.update(session, vo);
			tx.commit();
 		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally {
			this.closeSession();
		}
		map = ReturnUtil.optHandle(flag);
		return map;
	}
	/**
	 * 删除 溯源管理信息
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> removeTsCodeForRecordById(Long id)throws Exception{
		Session session = this.getSession();
 		Transaction tx = session.beginTransaction();
 		boolean flag = false;
 		Map<String,Object> map = new HashMap<String,Object>();
 		try {
 			TsCodeForRecord vo=dao.get(session, id);
 			vo.setState("000002");
 			flag = dao.update(session, vo);
 			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally {
			this.closeSession();
		}
		map = ReturnUtil.optHandle(flag);
		return map;
	}
	/**
	 * 根据id查询溯源信息管理
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public TsCodeForRecord findTsCodeForRecordById(Long id)throws Exception{
		Session session = this.getSession();
		TsCodeForRecord vo =  new TsCodeForRecord();
		try {
			vo = dao.get(session, id);
			if(vo.getKindId()>0){
				TsKind temp=kindDao.get(session, vo.getKindId());
				vo.setKindName(temp.getName());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			this.closeSession();
		}
		return vo;
	}
	/**
	 * 绑定
	 * @param bean
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> bindCodeForTranceSource(TsCodeForRecord bean)throws Exception{
		Session session = this.getSession();
 		Transaction tx = session.beginTransaction();
 		boolean flag = false;
		Map<String,Object> map = new HashMap<String,Object>();
 		try {

 			int state = bean.getCodeState();
 			String orgIdremot = bean.getOrgId();
 			if(state==0){
 				map = ReturnUtil.optCodeErrorHandle(bean.getCode()+"不存在!");
 			}else{
 				TsCodeForRecord vo=dao.get(session, bean.getId());
 				String orgId = vo.getOrgId();
 				if(!orgIdremot.equalsIgnoreCase(orgId)){//修改提示信息  lx 20120327
 					map = ReturnUtil.optCodeErrorHandle(bean.getCode()+"不属于当前企业，请修改!");
 				}
 				else{
 					//即使分配给了自己也不代表可以用,说不定已经用过了
 					boolean isExist=dao.codeByUsedJudge(session, bean.getCode());
 					if(isExist){
 						//存在
 						map = ReturnUtil.optCodeErrorHandle(bean.getCode()+"已经被使用，不能重复绑定!");
 					}else{
 						vo.setCode(bean.getCode());
 						flag = dao.update(session, vo);
 						tx.commit();
 						map = ReturnUtil.optHandle(flag);
 					}
 				}
 			}
		} catch (Exception e) {
			tx.rollback();
			throw e;
		}finally {
			this.closeSession();
		}
		return map;
	}
	
}
